package TwoClientGameConnection;

//The jdom package was used in constructing the XML messages.
import java.util.Vector;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 * A class that creates a message XML stream from valid TCGMessage objects.
 * @author Jace Ferguson
 * @filename TCGMessageCreator.java
 */
public class TCGMessageCreator extends Document{
	Element root;
	Element recipients;
	Element command;
	Element parameters;
	Element type;

	/**
	 * Constructor that takes in a message id to create a blank message string.
	 * @param messageId
	 */
	public TCGMessageCreator(Integer messageId)
	{
		super();
		this.setupDocument();
		this.setMessageId(messageId.toString());		 
	}

	/**
	 * Constructor takes in a TCGMessage object and creates a XML message string.
	 * @param message	TCGMessage	message object
	 */
	public TCGMessageCreator(TCGMessage message)
	{
		super();
		this.setupDocument();
		this.setMessageId(message.getMessageId().toString());
		this.setCommand(message.getCommand());
		this.setParameters(message.getParameters());		 
	}

	/**
	 * Add the parameter nodes to the document
	 * @param ParamName	String
	 * @param ParamValue	String
	 */
	public void addParameter(String ParamName, String ParamValue)
	{
		Element parameter;
		this.parameters.addContent(parameter = new Element("parameter"));
		parameter.setAttribute("ParamName", ParamName);
		parameter.addContent(ParamValue);
	}

	/**
	 * Get a string version of the message and return it.
	 * @return	String	XML message
	 */
	public String getStringMessage()
	{
		String Message;
		XMLOutputter outp = new XMLOutputter();
		Format format = Format.getCompactFormat();
		outp.setFormat(format);
		Message = outp.outputString(this);
		return Message;		 
	}

	/**
	 * Set the command string of the message
	 * @param command	String
	 */
	public void setCommand(String command)
	{
		this.command.setText(command);
	}

	/**
	 * Set the message id attribute for the message.
	 * @param id	Message id
	 */
	public void setMessageId(String id)
	{
		this.root.setAttribute("id", id);
	}

	/**
	 * Specify message type of the message. If the type is not valid
	 * set it to the default type.
	 * @param type	Integer type
	 */
	public void setMessageType(Integer type)
	{
		if(type > 3 || type < 0)
		{
			this.type.setText(String.valueOf(TCGMessage.MESSAGE_TYPE_NORMAL));
		}
		else
		{
			this.type.setText(type.toString());
		}
	}

	/**
	 * Set the parameters of the message with a vector of parameters
	 * @param parameters	Vector<parameter>
	 */
	public void setParameters(Vector<parameter> parameters)
	{
		for(parameter param : parameters)
		{
			this.addParameter(param.getParamName(), param.getParamValue());
		}
	}

	/**
	 * Setup the basic structure of the XML message
	 * and store specific nodes to the object so other nodes can
	 * be added.
	 */
	public void setupDocument()
	{
		this.root = new Element("TCGMessage");
		this.addContent(this.root);
		this.root.addContent(this.type = new Element("type").setText(String.valueOf(TCGMessage.MESSAGE_TYPE_NORMAL)));
		this.root.addContent(this.command = new Element("command"));
		this.root.addContent(this.parameters = new Element("parameters"));
	}

}


